import os
import sys
import django

DJANGO_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(DJANGO_PROJECT_ROOT)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BookDjango.settings')
django.setup()

from books.models import D_Books
import pandas as pd


df = pd.read_csv('data_clear.csv')
insertdata = [
    D_Books(
        title=row['标题'],
        summary=row['简介'],
        author=row['作者'],
        fav_count=row['收藏数'],
        commend_count=row['推荐数'],
        publisher=row['出版社'],
        price=row['价格'],
        tags=row['标签'],
        url=row['URL']
    )
    for _, row in df.iterrows()
]
# 批量插入数据
D_Books.objects.bulk_create(insertdata)